<?php

    function debug($variable, $die = true){
        $str = "<pre>".  print_r($variable, true) ."</pre>";
        if($die) 
            die($str);
        else 
            echo $die;
    }
    
    function retrieve_request(&$data, $vars = array(), $method = "post", $replace_enter = ""){
        $CI = &get_instance();        
        foreach ($vars as $key => $default) {
            $request = $CI->input->$method($key);            
            if(empty($request))
                $data[$key] = $default;
            else {
                $request = str_ireplace("\r\n", "<br>", $request);
                $data[$key] = $request;            
            }
        }
    }
    
    function terbilang($x){
        $abil = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
        if ($x < 12)
          return " " . $abil[$x];
        elseif ($x < 20)
          return terbilang($x - 10) . "belas";
        elseif ($x < 100)
          return terbilang($x / 10) . " puluh" . terbilang($x % 10);
        elseif ($x < 200)
          return " seratus" . terbilang($x - 100);
        elseif ($x < 1000)
          return terbilang($x / 100) . " ratus" . terbilang($x % 100);
        elseif ($x < 2000)
          return " seribu" . terbilang($x - 1000);
        elseif ($x < 1000000)
          return terbilang($x / 1000) . " ribu" . terbilang($x % 1000);
        elseif ($x < 1000000000)
          return terbilang($x / 1000000) . " juta" . terbilang($x % 1000000);
    }

    function said($number){
        $max_size = pow(10,18);
        if (!$number) return "zero";
        if (!is_nan($number) && $number < abs($max_size)) 
        {            
            switch ($number) 
            {
                // set up some rules for converting digits to words
                case $number < 0:
                    $prefix = "negative";
                    $suffix = said(-1*$number);
                    $string = $prefix . " " . $suffix;
                    break;
                case 1:
                    $string = "one";
                    break;
                case 2:
                    $string = "two";
                    break;
                case 3:
                    $string = "three";
                    break;
                case 4: 
                    $string = "four";
                    break;
                case 5:
                    $string = "five";
                    break;
                case 6:
                    $string = "six";
                    break;
                case 7:
                    $string = "seven";
                    break;
                case 8:
                    $string = "eight";
                    break;
                case 9:
                    $string = "nine";
                    break;                
                case 10:
                    $string = "ten";
                    break;            
                case 11:
                    $string = "eleven";
                    break;            
                case 12:
                    $string = "twelve";
                    break;            
                case 13:
                    $string = "thirteen";
                    break;            
                // fourteen handled later
                case 15:
                    $string = "fifteen";
                    break;            
                case $number < 20:
                    $string = said($number%10);
                    // eighteen only has one "t"
                    if ($number == 18)
                    {
                    $suffix = "een";
                    } else 
                    {
                    $suffix = "teen";
                    }
                    $string .= $suffix;
                    break;            
                case 20:
                    $string = "twenty";
                    break;            
                case 30:
                    $string = "thirty";
                    break;            
                case 40:
                    $string = "forty";
                    break;            
                case 50:
                    $string = "fifty";
                    break;            
                case 60:
                    $string = "sixty";
                    break;            
                case 70:
                    $string = "seventy";
                    break;            
                case 80:
                    $string = "eighty";
                    break;            
                case 90:
                    $string = "ninety";
                    break;                
                case $number < 100:
                    $prefix = said($number-$number%10);
                    $suffix = said($number%10);
                    $string = $prefix . "-" . $suffix;
                    break;
                // handles all number 100 to 999
                case $number < pow(10,3):                    
                    // floor return a float not an integer
                    $prefix = said(intval(floor($number/pow(10,2)))) . " hundred";
                    if ($number%pow(10,2)) $suffix = " and " . said($number%pow(10,2));
                    $string = $prefix . $suffix;
                    break;
                case $number < pow(10,6):
                    // floor return a float not an integer
                    $prefix = said(intval(floor($number/pow(10,3)))) . " thousand";
                    if ($number%pow(10,3)) $suffix = said($number%pow(10,3));
                    $string = $prefix . " " . $suffix;
                    break;
                case $number < pow(10,9):
                    // floor return a float not an integer
                    $prefix = said(intval(floor($number/pow(10,6)))) . " million";
                    if ($number%pow(10,6)) $suffix = said($number%pow(10,6));
                    $string = $prefix . " " . $suffix;
                    break;                    
                case $number < pow(10,12):
                    // floor return a float not an integer
                    $prefix = said(intval(floor($number/pow(10,9)))) . " billion";
                    if ($number%pow(10,9)) $suffix = said($number%pow(10,9));
                    $string = $prefix . " " . $suffix;    
                    break;
                case $number < pow(10,15):
                    // floor return a float not an integer
                    $prefix = said(intval(floor($number/pow(10,12)))) . " trillion";
                    if ($number%pow(10,12)) $suffix = said($number%pow(10,12));
                    $string = $prefix . " " . $suffix;    
                    break;        
                // Be careful not to pass default formatted numbers in the quadrillions+ into this function
                // Default formatting is float and causes errors
                case $number < pow(10,18):
                    // floor return a float not an integer
                    $prefix = said(intval(floor($number/pow(10,15)))) . " quadrillion";
                    if ($number%pow(10,15)) $suffix = said($number%pow(10,15));
                    $string = $prefix . " " . $suffix;    
                    break;                    
            }
        } else
        {
            echo "ERROR with - $number<br/> Number must be an integer between -" . number_format($max_size, 0, ".", ",") . " and " . number_format($max_size, 0, ".", ",") . " exclussive.";
        }
        return $string;    
    }
    
    function _debug($var){
        die("<pre>".print_r($var, true)."</pre>");
    }
?>
